package cc.yiueil.Interrupt;

/**
 * InterruptByFlag
 *
 * @author Administrator
 * @version 1.0
 */
public class InterruptByFlag {
    public static void main(String[] args) throws InterruptedException {
        HelloRunnable helloRunnable = new HelloRunnable();
        Thread t = new Thread(helloRunnable);
        t.start();
        // 暂停1毫秒 给子线程腾出资源
        Thread.sleep(1);
        // 中断t线程
        helloRunnable.flag = false;
        // 等待t线程结束
        t.join();
        System.out.println("end");
    }
}

class HelloRunnable implements Runnable {
    public volatile boolean flag = true;
    @Override
    public void run() {
        int n = 0;
        while (flag) {
            n++;
            System.out.println(n + " hello!");
        }
        System.out.println("thread end");
    }
}
